﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//请先添加引用

using Gift.Model;
namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类Car。
    /// </summary>
    public class Car
    {
        public Car()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbCar");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(tbCar model)
        {
            string strSql = " if exists(select 1 from tbCar where OrderCode = @OrderCode and ProductId = @ProductId and isnull(Contents,'') = @Contens) "
                            + " begin"
                            + " update tbCar set ProdNumber = ProdNumber + @ProdNumber ,ProdPrice = @ProdPrice,UpdateDate = getdate()"
                            + " where  OrderCode = @OrderCode and ProductId = @ProductId and  isnull(Contents,0) = @Contens"
                            + " end "
                            + " else"
                            + " begin"
                            + " insert into tbCar(OrderCode,ProductId,ProdNumber,ProdPrice,UpdateDate, Contents )"
                            + " values(@OrderCode,@ProductId,@ProdNumber,@ProdPrice,getdate(),@Contens)"
                            + " end ";
            SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.NVarChar,50),
					new SqlParameter("@ProductId", SqlDbType.Int,4),
					new SqlParameter("@ProdNumber", SqlDbType.Int,4),
					new SqlParameter("@ProdPrice", SqlDbType.Real,4),
                    new SqlParameter("@Contens", SqlDbType.NVarChar,100),                 };
            parameters[0].Value = model.OrderCode;
            parameters[1].Value = model.ProductId;
            parameters[2].Value = model.ProdNumber;
            parameters[3].Value = model.ProdPrice;
            parameters[4].Value = model.Conents;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(tbCar model)
        {
            string strSql = "if exists(select 1 from tbCar where OrderCode = @OrderCode and ProductId = @ProductId ) "
                        + " begin"
                        + " delete from tbCar where OrderCode = @OrderCode and ProductId = @ProductId; "
                        + " end "
                        + " insert into tbCar(OrderCode,ProductId,ProdNumber,ProdPrice,UpdateDate)"
                        + " values(@OrderCode,@ProductId,@ProdNumber,@ProdPrice,getdate())";
            SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.NVarChar,50),
					new SqlParameter("@ProductId", SqlDbType.Int,4),
					new SqlParameter("@ProdNumber", SqlDbType.Int,4),
					new SqlParameter("@ProdPrice", SqlDbType.Real,4)};
            parameters[0].Value = model.OrderCode;
            parameters[1].Value = model.ProductId;
            parameters[2].Value = model.ProdNumber;
            parameters[3].Value = model.ProdPrice;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public decimal GetSumPriceByProdId(int ProductId, int ProfessionAreaTypeId,string sContents)
        {
            string sSQL = " select sum(c.ProdNumber * c.ProdPrice) as SumPriceByPordId"
                          + " from tbCar c join DB_Shoppro s on c.ProductId = s.proid "
                          + " join tbProfessionAreaType p on s.ProfessionAreaTypeId = p.Id"
                          + " where c.ProductId != @ProductId and Contents = @Contents and s.ProfessionAreaTypeId = @ProfessionAreaTypeId ";
            SqlParameter[] parameters = {
					new SqlParameter("@ProductId", SqlDbType.Int)
                    ,new SqlParameter("@ProfessionAreaTypeId", SqlDbType.Int)
                    ,new SqlParameter("@Contents", SqlDbType.NVarChar,100)};
            parameters[0].Value = ProductId;
            parameters[1].Value = ProfessionAreaTypeId;
            SqlDataReader dr = DbHelperSQL.ExecuteReader(sSQL, parameters);
            if (dr.Read())
            {

                decimal dResult =  decimal.Parse(dr[0].ToString() == "" ? "0" : dr[0].ToString());
                dr.Close();
                return dResult;
            }
            else
            {
                dr.Close();
                return 0;
            }
        }



        public decimal GetSpendMoney(string sOrderCode, int ProfessionAreaTypeId)
        {
            string sSQL = "select isnull(sum(ProdNumber * ProdPrice),0) as AmountPrice from "
                        + " (select Id,OrderCode,ProductId,ProdNumber,ProdPrice,UpdateDate from tbCar c join DB_Shoppro s on c.ProductId = s.proid "
                        + " where c.OrderCode = @OrderCode and s.ProfessionAreaTypeId = @ProfessionAreaTypeId)a ";
            SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.NVarChar,50)
                    ,new SqlParameter("@ProfessionAreaTypeId", SqlDbType.Int)};
            parameters[0].Value = sOrderCode;
            parameters[1].Value = ProfessionAreaTypeId;
            SqlDataReader dr = DbHelperSQL.ExecuteReader(sSQL, parameters);
            if (dr.Read())
            {

                decimal dResult = decimal.Parse(dr[0].ToString());
                dr.Close();
                return dResult;
            }
            else
            {
                dr.Close();
                return 0;
            }

        }

        public decimal GetSpendTotalPrice(string sOrderCode)
        {
            string sSQL = "select isnull(sum(ProdNumber * ProdPrice),0) as AmountPrice from "
                        + "  tbCar  "
                        + " where OrderCode = @OrderCode  ";
            SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.NVarChar,50)};
            parameters[0].Value = sOrderCode;
            SqlDataReader dr = DbHelperSQL.ExecuteReader(sSQL, parameters);
            if (dr.Read())
            {

                decimal dResult = decimal.Parse(dr[0].ToString());
                dr.Close();
                return dResult;
            }
            else
            {
                dr.Close();
                return 0;
            }

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete tbCar ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(String sOrderCode,int iProductId,string sContents)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete tbCar ");
            strSql.Append(" where OrderCode = @OrderCode and ProductId = @ProductId and isnull(Contents,'') = @Contents");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@ProductId", SqlDbType.Int,4),
                     new SqlParameter("@Contents", SqlDbType.NVarChar,100)   };
            parameters[0].Value = sOrderCode;
            parameters[1].Value = iProductId;
            parameters[2].Value = sContents;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public tbCar GetModel(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,OrderCode,ProductId,ProdNumber,ProdPrice,UpdateDate from tbCar ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            tbCar model = new tbCar();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.OrderCode = ds.Tables[0].Rows[0]["OrderCode"].ToString();
                if (ds.Tables[0].Rows[0]["ProductId"].ToString() != "")
                {
                    model.ProductId = int.Parse(ds.Tables[0].Rows[0]["ProductId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProdNumber"].ToString() != "")
                {
                    model.ProdNumber = int.Parse(ds.Tables[0].Rows[0]["ProdNumber"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProdPrice"].ToString() != "")
                {
                    model.ProdPrice = decimal.Parse(ds.Tables[0].Rows[0]["ProdPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UpdateDate"].ToString() != "")
                {
                    model.UpdateDate = DateTime.Parse(ds.Tables[0].Rows[0]["UpdateDate"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,OrderCode,ProductId,ProdNumber,ProdPrice,UpdateDate ");
            strSql.Append(" FROM tbCar ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataTable GetListByOrderCode(string orderCode)
        {
            string sSQL = "select c.OrderCode,s.proid,sum(c.ProdNumber) as ProdNumber,c.ProdPrice,(sum(c.ProdNumber) * c.ProdPrice) as SumPrice,s.proname,s.ProfessionAreaTypeId,p.[Name] as ProfessionTypeName,c.Contents"
                             + " from tbCar c join DB_Shoppro s on c.ProductId = s.proid "
                              + " join tbProfessionAreaType p on s.ProfessionAreaTypeId = p.Id"
                              + " where c.OrderCode = @OrderCode"
                              + " group by c.OrderCode,s.proid,c.ProdPrice,s.proname,s.ProfessionAreaTypeId,p.[Name],c.Contents";
            SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.Int,4)};
            parameters[0].Value = orderCode;
            return DbHelperSQL.Query(sSQL, parameters).Tables[0];
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbCar";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}

